package com.push.web.controller;

import java.util.List;

import cn.hutool.core.util.IdUtil;
import com.push.common.config.RuoYiConfig;
import com.push.common.utils.DateUtils;
import com.push.common.utils.file.FileUtils;
import com.push.framework.config.ServerConfig;
import com.push.web.spider.WOSSpinder;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.push.common.annotation.Log;
import com.push.common.core.controller.BaseController;
import com.push.common.core.domain.AjaxResult;
import com.push.common.enums.BusinessType;
import com.push.web.domain.BizBibinfoWos;
import com.push.web.service.IBizBibinfoWosService;
import com.push.common.utils.poi.ExcelUtil;
import com.push.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * WOSController
 *
 * @author yanxi
 * @date 2022-07-27
 */
@RestController
@RequestMapping("/intelligence/wos")
public class BizBibinfoWosController extends BaseController
{
    @Autowired
    private IBizBibinfoWosService bizBibinfoWosService;

    @Autowired
    private ServerConfig serverConfig;

    /**
     * 查询WOS列表
     */
    @PreAuthorize("@ss.hasPermi('intelligence:wos:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizBibinfoWos bizBibinfoWos)
    {
        startPage();
        List<BizBibinfoWos> list = bizBibinfoWosService.selectBizBibinfoWosList(bizBibinfoWos);
        return getDataTable(list);
    }

    /**
     * 导出WOS列表
     */
    @PreAuthorize("@ss.hasPermi('intelligence:wos:export')")
    @Log(title = "WOS", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BizBibinfoWos bizBibinfoWos)
    {
        List<BizBibinfoWos> list = bizBibinfoWosService.selectBizBibinfoWosList(bizBibinfoWos);
        ExcelUtil<BizBibinfoWos> util = new ExcelUtil<BizBibinfoWos>(BizBibinfoWos.class);
        return util.exportExcel(list, "WOS数据");
    }

    /**
     * 获取WOS详细信息
     */
    @PreAuthorize("@ss.hasPermi('intelligence:wos:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(bizBibinfoWosService.selectBizBibinfoWosById(id));
    }

    /**
     * 新增WOS
     */
    @PreAuthorize("@ss.hasPermi('intelligence:wos:add')")
    @Log(title = "WOS", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizBibinfoWos bizBibinfoWos)
    {
        bizBibinfoWos.setCreateDate(DateUtils.currentDate());
        return toAjax(bizBibinfoWosService.insertBizBibinfoWos(bizBibinfoWos));
    }

    /**
     * 修改WOS
     */
    @PreAuthorize("@ss.hasPermi('intelligence:wos:edit')")
    @Log(title = "WOS", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizBibinfoWos bizBibinfoWos)
    {
        return toAjax(bizBibinfoWosService.updateBizBibinfoWos(bizBibinfoWos));
    }

    /**
     * 删除WOS
     */
    @PreAuthorize("@ss.hasPermi('intelligence:wos:remove')")
    @Log(title = "WOS", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bizBibinfoWosService.deleteBizBibinfoWosByIds(ids));
    }

    /**
     * 修改信息管理
     */
    @PreAuthorize("@ss.hasPermi('information:article:updateCount')")
    @PostMapping("/updateCount")
    public AjaxResult updateCount(@RequestBody BizBibinfoWos bizBibinfoWos)
    {
        bizBibinfoWos = bizBibinfoWosService.selectBizBibinfoWosById(bizBibinfoWos.getId());
        WOSSpinder wosSpinder = new WOSSpinder();
        bizBibinfoWos.setResultNum(wosSpinder.getWosResult(bizBibinfoWos.getSearchUrl()));
        return toAjax(bizBibinfoWosService.updateBizBibinfoWos(bizBibinfoWos));
    }

    /**
     * 修改信息管理
     */
    @PreAuthorize("@ss.hasPermi('information:article:getWosData')")
    @PostMapping("/getWosData")
    public AjaxResult getWosData(@RequestBody BizBibinfoWos bizBibinfoWos)
    {
        bizBibinfoWos = bizBibinfoWosService.selectBizBibinfoWosById(bizBibinfoWos.getId());
        WOSSpinder wosSpinder = new WOSSpinder();
        String fileName = IdUtil.simpleUUID()+".txt";
        String filePath = RuoYiConfig.getUploadPath() + "/" + fileName;
        bizBibinfoWos.setResultFile(filePath);
        //http://localhost/dev-api/profile/upload/a70506a05e0a4f24a912cac15f8af4af.txt
        wosSpinder.getWosData(bizBibinfoWos.getSearchUrl(), RuoYiConfig.getProfile(), filePath);
        return toAjax(bizBibinfoWosService.updateBizBibinfoWos(bizBibinfoWos));
    }

    @GetMapping("/downloadFile/{id}")
    public void downloadFile(@PathVariable("id") long id, HttpServletResponse response, HttpServletRequest request) throws Exception
    {
        BizBibinfoWos bizBibinfoWos = bizBibinfoWosService.selectBizBibinfoWosById(id);
        String filePath = bizBibinfoWos.getResultFile();
        String realFileName = filePath.substring(filePath.lastIndexOf("/")+1) + filePath.substring(filePath.indexOf("."));
        System.out.println("realFileName:"+realFileName);
        String path = RuoYiConfig.getUploadPath() + filePath;
        response.setCharacterEncoding("utf-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition",
                "attachment;fileName=" + FileUtils.setFileDownloadHeader(request, realFileName));
        FileUtils.writeBytes(path, response.getOutputStream());
    }
}
